package com.example.demo.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.entity.AfterSaleTargetZ1;
import com.example.demo.entity.AfterSaleTargetZ2;
import com.example.demo.entity.AfterSalefront;
import com.example.demo.entity.AfterSalefront2;
import com.example.demo.entity.AftersalesWarehouse;
import com.example.demo.entity.CarKeep;
import com.example.demo.entity.CarKeep2;
import com.example.demo.entity.CarNumberCompany1;
import com.example.demo.entity.CarNumberCompany2;
import com.example.demo.entity.Insurance;
import com.example.demo.entity.Insurance2;
import com.example.demo.entity.SaleBoutique1;
import com.example.demo.entity.SaleBoutique2;
import com.example.demo.entity.SysUser;
import com.example.demo.entity.XiMei;
import com.example.demo.entity.XiMei2;
import com.example.demo.entity.YanBao;
import com.example.demo.entity.YanBao2;

@Service
public class AfterSaleYanShengComputerService {
	@Autowired
	private CarNumberCompanyService2 carNumService2;
	@Autowired
	private AfterSalefront2Service after2Service2;
	@Autowired
	private AfterSalefrontService afterService1;
	@Autowired
	private Insurance2Service baoXianService2;
	@Autowired
	private YanBao2Service yanbaoService2;
	@Autowired
	private SaleBoutique2Service saleBoutique2Service;
	@Autowired
	private AftersalesWarehouseService cangKuService;
	@Autowired
	private CarKeep2Service carKeep2Service;
	@Autowired
	private XiMei2Service ximei2Service;
	@Autowired
	private AfterSaleTargetZ2Service MuBiaoservice2;
	@Autowired
	private AfterSaleTargetZ1Service MuBiaoservice1;

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后服务衍生收入合计(续保交强险收入+续保商业险收入+售后延保收入+售后代办年审收入)
	 */
	public Float ShouHouFuWuYanShengShouRuHeJi(SysUser user, Date beginDate, Date endDate) {
		return XvBaoJiaoQiangXianShouRu(user, beginDate, endDate) + XvBaoShangYeXianShouRu(user, beginDate, endDate)
				+ ShouHouYanBaoShouRu(user, beginDate, endDate) + ShouHoudaiBanNianShenShouRu(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后服务衍生成本合计(续保交强险成本+续保商业险成本+售后延保成本+售后代办年审成本)
	 */
	public Float ShouHouFuWuYanShengChengBenHeJi(SysUser user, Date beginDate, Date endDate) {
		return XvBaoJiaoQiangXianChengBen(user, beginDate, endDate) + XvBaoShangYeXianChengBen(user, beginDate, endDate)
				+ ShouHouYanBaoChengBen(user, beginDate, endDate)
				+ ShouHouDaiBanNianShenChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后服务衍生毛利合计(续保交强险毛利+续保商业险毛利+售后延保毛利+售后代办年审毛利)
	 */
	public Float ShouHouFuWuYanShengMaoLiHeJi(SysUser user, Date beginDate, Date endDate) {
		return XvBaoJiaoQiangXianMaoLi(user, beginDate, endDate) + XvBaoShangYeXianMaoLi(user, beginDate, endDate)
				+ ShouHouYanBaoMaoLi(user, beginDate, endDate) + ShouHouDaiBanNianShenMaoLi(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后服务衍生毛利率
	 */
	public Float ShouHouFuWuYanShengMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouFuWuYanShengShouRuHeJi(user, beginDate, endDate) != 0) {
			return ShouHouFuWuYanShengMaoLiHeJi(user, beginDate, endDate)
					/ ShouHouFuWuYanShengShouRuHeJi(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后专业衍生收入合计(售后精品销售收入+售后养护品收入+售后保养套餐收入+售后洗美收入)
	 */
	public Float ShouHouZhuanYeYanShengShouRuHeJi(SysUser user, Date beginDate, Date endDate) {
		return ShouHouJingPinXiaoShouShouRu(user, beginDate, endDate) + ShouHouYangHuPinShouRu(user, beginDate, endDate)
				+ ShouHouBaoYangTaoCanShouRu(user, beginDate, endDate) + ShouHouXiMeiShouRu(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后专业衍生毛利合计(售后精品销售毛利+售后养护品毛利+售后保养套餐毛利+售后洗美毛利)
	 */
	public Float ShouHouZhuanYeYanShengMaoLiHeJi(SysUser user, Date beginDate, Date endDate) {
		return ShouHouJingPinXiaoShouMaoLi(user, beginDate, endDate) + ShouHouYangHuPinMaoLi(user, beginDate, endDate)
				+ ShouHouBaoYangTaoCanMaoLi(user, beginDate, endDate) + ShouHouXiMeiMaoLi(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后专业衍生成本合计(售后精品销售成本+售后养护品成本+售后保养套餐成本+售后洗美成本)
	 */
	public Float ShouHouZhuanYeYanShengChengBenHeJi(SysUser user, Date beginDate, Date endDate) {
		return ShouHouJingPinXiaoShouChengBen(user, beginDate, endDate)
				+ ShouHouYangHuPinChengBen(user, beginDate, endDate)
				+ ShouHouBaoYangTaoCanChengBen(user, beginDate, endDate)
				+ ShouHouXiMeiChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后专业衍生毛利率
	 */
	public Float ShouHouZhuanYeYanShengMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		if(ShouHouZhuanYeYanShengShouRuHeJi(user, beginDate, endDate)!=0) {
			return ShouHouZhuanYeYanShengMaoLiHeJi(user, beginDate, endDate)/ShouHouZhuanYeYanShengShouRuHeJi(user, beginDate, endDate);
		}else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保交强险收入(续保交强险毛利+续保交强险成本)
	 */
	public Float XvBaoJiaoQiangXianShouRu(SysUser user, Date beginDate, Date endDate) {
		return XvBaoJiaoQiangXianMaoLi(user, beginDate, endDate) + XvBaoJiaoQiangXianChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @return 续保交强险成本(录入交强险金额-交强险毛利)
	 */
	public Float XvBaoJiaoQiangXianChengBen(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		Float maoli = XvBaoJiaoQiangXianMaoLi(user, beginDate, endDate);
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 cars2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (cars2.getMaiJiaoQiang() != null) {
				num += cars2.getMaiJiaoQiang();
			} else {
				continue;
			}
		}

		return num - maoli;
	}

	/**
	 * @param user
	 * @return 续保交强险毛利(录入交强险金额*交强险返利点)
	 */
	public Float XvBaoJiaoQiangXianMaoLi(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 cars2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (cars2.getMaiJiaoQiang() != null) {
				List<Insurance> baoXian = cars2.getMaiBaoXian();
				if (baoXian.size() > 0) {
					Insurance baoxian = baoXian.get(0);
					Insurance2 baoXian2 = findByBaoXian(baoxian, beginDate, endDate);
					Float jqxfld = baoXian2.getJqxfld();
					num += cars2.getMaiJiaoQiang() * jqxfld;
				} else {
					continue;
				}
			} else {
				continue;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保交强险毛利率(续保交强险毛利/续保交强险收入)
	 */
	public Float XvBaoJiaoQiangXianMaoLiLv(SysUser user, Date beginDate, Date endDate) {

		if (XvBaoJiaoQiangXianShouRu(user, beginDate, endDate) != 0) {

			return XvBaoJiaoQiangXianMaoLi(user, beginDate, endDate)
					/ XvBaoJiaoQiangXianShouRu(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @return 售后代办年审毛利(录入金额*年审返利点)
	 */
	public Float ShouHouDaiBanNianShenMaoLi(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 cars2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (cars2.getXiaoShouChePai() != null) {
				List<CarNumberCompany1> baoXian = cars2.getMaiChePai();
				if (baoXian.size() > 0) {
					CarNumberCompany1 baoxian = baoXian.get(0);
					CarNumberCompany2 baoXian2 = findByChePai(baoxian, beginDate, endDate);
					Float jqxfld = baoXian2.getYearExamineBackMoney();
					num += cars2.getXiaoShouChePai() * jqxfld;
				} else {
					continue;
				}
			} else {
				continue;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 售后代办年审成本(录入车牌金额-车牌毛利)
	 */
	public Float ShouHouDaiBanNianShenChengBen(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 cars2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (cars2.getXiaoShouChePai() != null) {
				num += cars2.getXiaoShouChePai();
			}
		}
		return num - ShouHouDaiBanNianShenMaoLi(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后代办年审收入（成本+毛利）
	 */
	public Float ShouHoudaiBanNianShenShouRu(SysUser user, Date beginDate, Date endDate) {
		return ShouHouDaiBanNianShenChengBen(user, beginDate, endDate)
				+ ShouHouDaiBanNianShenMaoLi(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后代办年审毛利率（毛利/收入）
	 */
	public Float ShouHouDaiBanNianShenMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHoudaiBanNianShenShouRu(user, beginDate, endDate) != 0) {
			return ShouHouDaiBanNianShenMaoLi(user, beginDate, endDate)
					/ ShouHoudaiBanNianShenShouRu(user, beginDate, endDate);

		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @return 续保商业险成本(录入商业险金额-商业险毛利)
	 */
	public Float XvBaoShangYeXianChengBen(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		Float maoli = XvBaoShangYeXianMaoLi(user, beginDate, endDate);
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 cars2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (cars2.getMaiShangYeXian() != null) {
				num += cars2.getMaiShangYeXian();
			} else {
				continue;
			}
		}

		return num - maoli;
	}

	/**
	 * @param user
	 * @return 续保商业险毛利(录入商业险金额*商业险返利点)
	 */
	public Float XvBaoShangYeXianMaoLi(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 cars2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (cars2.getMaiShangYeXian() != null) {
				List<Insurance> baoXian = cars2.getMaiBaoXian();
				if (baoXian.size() > 0) {
					Insurance baoxian = baoXian.get(0);
					Insurance2 baoXian2 = findByBaoXian(baoxian, beginDate, endDate);
					Float jqxfld = baoXian2.getSyxfld();
					num += cars2.getMaiShangYeXian() * jqxfld;
				} else {
					continue;
				}
			} else {
				continue;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保商业险收入(续保商业险毛利+续保商业险成本)
	 */
	public Float XvBaoShangYeXianShouRu(SysUser user, Date beginDate, Date endDate) {
		return XvBaoShangYeXianChengBen(user, beginDate, endDate) + XvBaoShangYeXianMaoLi(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保交强险毛利率(续保交强险毛利/续保交强险收入)
	 */
	public Float XvBaoShangYeXianMaoLiLv(SysUser user, Date beginDate, Date endDate) {

		if (XvBaoShangYeXianShouRu(user, beginDate, endDate) != 0) {

			return XvBaoShangYeXianMaoLi(user, beginDate, endDate) / XvBaoShangYeXianShouRu(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后延保收入
	 */
	public Float ShouHouYanBaoShouRu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront afterSalefront : list) {
			AfterSalefront2 salefront2 = findByAfterSale1(afterSalefront, beginDate, endDate);
			List<YanBao> maiYanBao = salefront2.getMaiYanBao();
			if (maiYanBao.size() > 0) {
				YanBao yanBao = maiYanBao.get(0);
				YanBao2 yanBao2 = findByYanBao(yanBao, beginDate, endDate);
				num = yanBao2.getYanBaoPrice();
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后延保成本(延保成本)
	 */
	public Float ShouHouYanBaoChengBen(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			List<YanBao> jingPin = AfterSalefront2.getMaiYanBao();
			if (jingPin.size() > 0) {
				YanBao YanBao1 = jingPin.get(0);
				YanBao2 salebou2 = findByYanBao(YanBao1, beginDate, endDate);
				num = salebou2.getYanBaoCost();
			}
		}
		return num;

	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后延保毛利
	 */
	public Float ShouHouYanBaoMaoLi(SysUser user, Date beginDate, Date endDate) {
		return ShouHouYanBaoShouRu(user, beginDate, endDate) - ShouHouYanBaoChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后延保毛利率（延保毛利/延保收入）
	 */
	public Float ShouHouYanBaoMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouYanBaoShouRu(user, beginDate, endDate) != 0) {
			return ShouHouYanBaoMaoLi(user, beginDate, endDate) / ShouHouYanBaoShouRu(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后精品销售收入(精品材料价格+精品工时价格)
	 */
	public Float ShouHouJingPinXiaoShouShouRu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			List<SaleBoutique1> jingPin = AfterSalefront2.getMaiJingPin();
			if (jingPin.size() > 0) {
				SaleBoutique1 saleBoutique1 = jingPin.get(0);
				SaleBoutique2 salebou2 = findByJingPin(saleBoutique1, beginDate, endDate);
				num = salebou2.getCaiLiaoPrice() + salebou2.getGongShiPrice();
			}
		}
		return num;

	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后洗美收入(洗美材料价格+洗美工时价格)
	 */
	public Float ShouHouXiMeiShouRu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			List<XiMei> jingPin = AfterSalefront2.getMaiXiMei();
			if (jingPin.size() > 0) {
				XiMei XiMei1 = jingPin.get(0);
				XiMei2 salebou2 = findByXiMei(XiMei1, beginDate, endDate);
				num = salebou2.getCaiLiaoPrice() + salebou2.getGongShiPrice();
			}
		}
		return num;

	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后精品销售成本(精品材料成本+精品工时成本)
	 */
	public Float ShouHouJingPinXiaoShouChengBen(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			List<SaleBoutique1> jingPin = AfterSalefront2.getMaiJingPin();
			if (jingPin.size() > 0) {
				SaleBoutique1 saleBoutique1 = jingPin.get(0);
				SaleBoutique2 salebou2 = findByJingPin(saleBoutique1, beginDate, endDate);
				num = salebou2.getCaiLiaoCost() + salebou2.getGongShiCost();
			}
		}
		return num;

	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后洗美成本(洗美材料成本+洗美工时成本)
	 */
	public Float ShouHouXiMeiChengBen(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			List<XiMei> jingPin = AfterSalefront2.getMaiXiMei();
			if (jingPin.size() > 0) {
				XiMei XiMei1 = jingPin.get(0);
				XiMei2 salebou2 = findByXiMei(XiMei1, beginDate, endDate);
				num = salebou2.getCaiLiaoCost() + salebou2.getGongShiCost();
			}
		}
		return num;

	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后精品销售毛利(售后精品销售收入-售后精品销售成本)
	 */
	public Float ShouHouJingPinXiaoShouMaoLi(SysUser user, Date beginDate, Date endDate) {
		return ShouHouJingPinXiaoShouShouRu(user, beginDate, endDate)
				- ShouHouJingPinXiaoShouChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后洗美毛利(售后洗美收入-售后洗美成本)
	 */
	public Float ShouHouXiMeiMaoLi(SysUser user, Date beginDate, Date endDate) {
		return ShouHouXiMeiShouRu(user, beginDate, endDate) - ShouHouXiMeiChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后精品销售毛利率(售后精品销售毛利/售后精品销售收入)
	 */
	public Float ShouHouJingPinXiaoShouMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouJingPinXiaoShouShouRu(user, beginDate, endDate) != 0) {

			return ShouHouJingPinXiaoShouMaoLi(user, beginDate, endDate)
					/ ShouHouJingPinXiaoShouShouRu(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后洗美毛利率(售后洗美毛利/售后洗美收入)
	 */
	public Float ShouHouXiMeiMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouXiMeiShouRu(user, beginDate, endDate) != 0) {
			return ShouHouXiMeiMaoLi(user, beginDate, endDate) / ShouHouXiMeiShouRu(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保交强险目标台数
	 */
	public Integer XvBaoJiaoQiangXianMuBiaoTaiShu(SysUser user, Date beginDate, Date endDate) {
		AfterSaleTargetZ1 tar1 = MuBiaoservice1.findByUser(user);
		AfterSaleTargetZ2 tar2 = findByRenYuanMuBiao(tar1, beginDate, endDate);
		return tar2.getMyContinueInsurance();
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保交强险实际台数
	 */
	public Integer XvBaoJiaoQiangXianShiJiTaiShu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		int num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getMaiJiaoQiang() > 0) {
				num++;
			} else {
				continue;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保商业险实际台数
	 */
	public Integer XvBaoShangYeXianShiJiTaiShu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		int num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getMaiShangYeXian() > 0) {
				num++;
			} else {
				continue;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保交强险目标达成率
	 */
	public Float XvBaoJiaoQiangXianMuBiaoDaChengLv(SysUser user, Date beginDate, Date endDate) {
		if (XvBaoJiaoQiangXianMuBiaoTaiShu(user, beginDate, endDate) != 0) {
			return (float) (XvBaoJiaoQiangXianShiJiTaiShu(user, beginDate, endDate)
					/ XvBaoJiaoQiangXianMuBiaoTaiShu(user, beginDate, endDate));
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保商业险目标达成率
	 */
	public Float XvBaoShangYeXianMuBiaoDaChengLv(SysUser user, Date beginDate, Date endDate) {
		if (XvBaoShangYeXianMuBiaoTaiShu(user, beginDate, endDate) != 0) {
			return (float) (XvBaoShangYeXianShiJiTaiShu(user, beginDate, endDate)
					/ XvBaoShangYeXianMuBiaoTaiShu(user, beginDate, endDate));
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 续保商业险目标台数
	 */
	public Integer XvBaoShangYeXianMuBiaoTaiShu(SysUser user, Date beginDate, Date endDate) {
		AfterSaleTargetZ1 tar1 = MuBiaoservice1.findByUser(user);
		AfterSaleTargetZ2 tar2 = findByRenYuanMuBiao(tar1, beginDate, endDate);
		return tar2.getShangYeXian();
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后延保销售目标台数
	 */
	public Integer ShouHouYanBaoXiaoShouMuBiaoTaiShu(SysUser user, Date beginDate, Date endDate) {
		AfterSaleTargetZ1 tar1 = MuBiaoservice1.findByUser(user);
		AfterSaleTargetZ2 tar2 = findByRenYuanMuBiao(tar1, beginDate, endDate);
		return tar2.getMyLengthenInsurance();
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后保养套餐销售目标台数
	 */
	public Integer ShouHouBaoYangTaoCanXiaoShouMuBiaoTaiShu(SysUser user, Date beginDate, Date endDate) {
		AfterSaleTargetZ1 tar1 = MuBiaoservice1.findByUser(user);
		AfterSaleTargetZ2 tar2 = findByRenYuanMuBiao(tar1, beginDate, endDate);
		return tar2.getMyMaintain();
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后代办年审目标台数
	 */
	public Integer ShouHouDaiBanNianShenMuBiaoTaiShu(SysUser user, Date beginDate, Date endDate) {
		AfterSaleTargetZ1 tar1 = MuBiaoservice1.findByUser(user);
		AfterSaleTargetZ2 tar2 = findByRenYuanMuBiao(tar1, beginDate, endDate);
		return tar2.getShouHouDaiBanNianShen();
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后洗美目标收入
	 */
	public Integer ShouHouXiMeiMuBiaoShouRu(SysUser user, Date beginDate, Date endDate) {
		AfterSaleTargetZ1 tar1 = MuBiaoservice1.findByUser(user);
		AfterSaleTargetZ2 tar2 = findByRenYuanMuBiao(tar1, beginDate, endDate);
		return tar2.getXiMei();
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后洗美实际收入
	 */
	public Float ShouHouXiMeiShiJiShouRU(SysUser user, Date beginDate, Date endDate) {
		return ShouHouXiMeiShouRu(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后洗美目标达成率
	 */
	public Float ShouHouXiMeiMuBiaoDaChengLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouXiMeiMuBiaoShouRu(user, beginDate, endDate) != 0) {
			return ShouHouXiMeiShiJiShouRU(user, beginDate, endDate)
					/ ShouHouXiMeiMuBiaoShouRu(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后代办年审实际台数
	 */
	public Integer ShouHouDaiBanNianShenShiJiTaiShu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		int num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getMaiChePai().size() > 0) {
				num++;
			} else {
				continue;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后保养套餐销售实际台数
	 */
	public Integer ShouHouBaoYangTaoCanShiJiTaiShu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		int num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getMaiBaoYang().size() > 0) {
				num++;
			} else {
				continue;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后保养套餐销售目标达成率
	 */
	public Float ShouHouBaoYangTaoCanXiaoLiangMuBiaoDaChengLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouBaoYangTaoCanXiaoShouMuBiaoTaiShu(user, beginDate, endDate) != 0) {
			return (float) (ShouHouBaoYangTaoCanShiJiTaiShu(user, beginDate, endDate)
					/ ShouHouBaoYangTaoCanXiaoShouMuBiaoTaiShu(user, beginDate, endDate));
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后延保销售实际台数
	 */
	public Integer ShouHouYanBaoXiaoShouShiJiTaiShu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		int num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getMaiYanBao().size() > 0) {
				num++;
			} else {
				continue;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后延保销售目标达成率
	 */
	public Float ShouHouYanBaoXiaoShouMuBiaoDaChengLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouYanBaoXiaoShouMuBiaoTaiShu(user, beginDate, endDate) != 0) {
			return (float) (ShouHouYanBaoXiaoShouShiJiTaiShu(user, beginDate, endDate)
					/ ShouHouYanBaoXiaoShouMuBiaoTaiShu(user, beginDate, endDate));
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后代办年审目标达成率
	 */
	public Float ShouHouDaiBanNianShenMuBiaoDaChengLV(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouDaiBanNianShenMuBiaoTaiShu(user, beginDate, endDate) != 0) {
			return (float) (ShouHouDaiBanNianShenShiJiTaiShu(user, beginDate, endDate)
					/ ShouHouDaiBanNianShenMuBiaoTaiShu(user, beginDate, endDate));
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后养护品收入
	 */
	public Float ShouHouYangHuPinShouRu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront afterSalefront : list) {
			AfterSalefront2 salefront2 = findByAfterSale1(afterSalefront, beginDate, endDate);
			if (salefront2.getYanghuPrice() != null) {
				num = salefront2.getYanghuPrice();
			} else {
				return num;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后养护品成本
	 */
	public Float ShouHouYangHuPinChengBen(SysUser user, Date beginDate, Date endDate) {
		List<AftersalesWarehouse> list = cangKuService.findByComeparts("养护品");
		AftersalesWarehouse after = new AftersalesWarehouse();
		Integer id = 0;
		for (AftersalesWarehouse warehouse : list) {
			if (id < warehouse.getId()) {
				id = warehouse.getId();
				after = cangKuService.findById(id);
			}
		}
		return (float) after.getComemoney();
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后养护品毛利
	 */
	public Float ShouHouYangHuPinMaoLi(SysUser user, Date beginDate, Date endDate) {
		return ShouHouYangHuPinShouRu(user, beginDate, endDate) - ShouHouYangHuPinChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后养护品毛利率
	 */
	public Float ShouHouYangHuPinMaoLiLV(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouYangHuPinShouRu(user, beginDate, endDate) != 0) {
			return ShouHouYangHuPinMaoLi(user, beginDate, endDate) / ShouHouYangHuPinShouRu(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后保养套餐收入
	 */
	public Float ShouHouBaoYangTaoCanShouRu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront afterSalefront : list) {
			AfterSalefront2 salefront2 = findByAfterSale1(afterSalefront, beginDate, endDate);
			List<CarKeep> maiCarKeep = salefront2.getMaiBaoYang();
			if (maiCarKeep.size() > 0) {
				CarKeep CarKeep = maiCarKeep.get(0);
				CarKeep2 CarKeep2 = findByBaoYang(CarKeep, beginDate, endDate);
				num = CarKeep2.getPrice();
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后精品销售实际收入
	 */
	public Float ShouHouJingPinXiaoShouShiJiShouRu(SysUser user, Date beginDate, Date endDate) {
		return ShouHouJingPinXiaoShouShouRu(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后精品销售目标达成率
	 */
	public Float ShouHouJingPinXiaoShouMuBiaoDaChengLV(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouJingPinXiaoShouMuBiaoShouRu(user, beginDate, endDate) != 0) {
			return ShouHouJingPinXiaoShouShiJiShouRu(user, beginDate, endDate)
					/ ShouHouJingPinXiaoShouMuBiaoShouRu(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后精品销售目标收入
	 */
	public Integer ShouHouJingPinXiaoShouMuBiaoShouRu(SysUser user, Date beginDate, Date endDate) {
		AfterSaleTargetZ1 tar1 = MuBiaoservice1.findByUser(user);
		AfterSaleTargetZ2 tar2 = findByRenYuanMuBiao(tar1, beginDate, endDate);
		return tar2.getMyBoutique();
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后养护品销售目标数量
	 */
	public Integer ShouHouYangHuPinXiaoShouMuBiaoShuLiang(SysUser user, Date beginDate, Date endDate) {
		AfterSaleTargetZ1 tar1 = MuBiaoservice1.findByUser(user);
		AfterSaleTargetZ2 tar2 = findByRenYuanMuBiao(tar1, beginDate, endDate);
		return tar2.getMyKeep();
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后养护品销售实际数量
	 */
	public Integer ShouHouYangHuPinXiaoShouShiJiShuLiang(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		int num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getYanghuPrice() > 0) {
				num++;
			} else {
				continue;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后养护品销售目标达成率
	 */
	public Float ShouHouYangHuPinXiaoShouMuBiaoDaChengLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouYangHuPinXiaoShouMuBiaoShuLiang(user, beginDate, endDate) != 0) {
			return (float) (ShouHouYangHuPinXiaoShouShiJiShuLiang(user, beginDate, endDate)
					/ ShouHouYangHuPinXiaoShouMuBiaoShuLiang(user, beginDate, endDate));
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后保养套餐成本(保养套餐成本)
	 */
	public Float ShouHouBaoYangTaoCanChengBen(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			List<CarKeep> jingPin = AfterSalefront2.getMaiBaoYang();
			if (jingPin.size() > 0) {
				CarKeep CarKeep1 = jingPin.get(0);
				CarKeep2 salebou2 = findByBaoYang(CarKeep1, beginDate, endDate);
				num = salebou2.getCost();
			}
		}
		return num;

	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后保养套餐毛利
	 */
	public Float ShouHouBaoYangTaoCanMaoLi(SysUser user, Date beginDate, Date endDate) {
		return ShouHouBaoYangTaoCanShouRu(user, beginDate, endDate)
				- ShouHouBaoYangTaoCanChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后延保毛利率（延保毛利/延保收入）
	 */
	public Float ShouHouBaoYangTaoCanMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouYanBaoShouRu(user, beginDate, endDate) != 0) {
			return ShouHouYanBaoMaoLi(user, beginDate, endDate) / ShouHouYanBaoShouRu(user, beginDate, endDate);
		} else {
			return (float) 0;
		}
	}

	/**
	 * 3 通过人员目标录入1表找二表唯一
	 * 
	 * @return
	 */
	public AfterSaleTargetZ2 findByRenYuanMuBiao(AfterSaleTargetZ1 car, Date beginDate, Date endDate) {
		List<AfterSaleTargetZ2> cars2 = MuBiaoservice2.findByUser1(car);
		if (cars2 != null) {
			AfterSaleTargetZ2 cars = new AfterSaleTargetZ2();
			Integer id = 1;
			for (AfterSaleTargetZ2 y : cars2) {
				if (id < y.getId()) {
					id = y.getId();
				}
			}
			cars = MuBiaoservice2.findById(id);
			boolean b = isEffectiveDate(cars.getBeginDate(), beginDate, endDate);
			if (b) {
				return cars;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * @param baoxian
	 * @param beginDate
	 * @param endDate
	 * @return 通过车牌公司1找到2
	 */
	private CarNumberCompany2 findByChePai(CarNumberCompany1 carNum1, Date beginDate, Date endDate) {
		List<CarNumberCompany2> baoXian2 = carNumService2.findByCarNumberCompany(carNum1);
		if (baoXian2 != null) {
			CarNumberCompany2 baoXian = new CarNumberCompany2();
			Integer id = 0;
			for (CarNumberCompany2 y : baoXian2) {
				if (id < y.getId()) {
					id = y.getId();
					baoXian = y;
				}
			}
			boolean b = isEffectiveDate(baoXian.getBeginDate(), beginDate, endDate);
			if (b) {
				return baoXian;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 通过保养套餐1表找到2表
	 * 
	 * @return
	 */
	public CarKeep2 findByBaoYang(CarKeep baoXian1, Date beginDate, Date endDate) {
		List<CarKeep2> baoXian2 = carKeep2Service.findByCarKeep(baoXian1);
		if (baoXian2 != null) {
			CarKeep2 baoXian = new CarKeep2();
			Integer id = 0;
			for (CarKeep2 y : baoXian2) {
				if (id < y.getId()) {
					id = y.getId();
					baoXian = y;
				}
			}
			boolean b = isEffectiveDate(baoXian.getBeginDate(), beginDate, endDate);
			if (b) {
				return baoXian;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 通过精品1表找到2表
	 * 
	 * @return
	 */
	public SaleBoutique2 findByJingPin(SaleBoutique1 baoXian1, Date beginDate, Date endDate) {
		List<SaleBoutique2> baoXian2 = saleBoutique2Service.findBySaleBoutique(baoXian1);
		if (baoXian2 != null) {
			SaleBoutique2 baoXian = new SaleBoutique2();
			Integer id = 0;
			for (SaleBoutique2 y : baoXian2) {
				if (id < y.getId()) {
					id = y.getId();
					baoXian = y;
				}
			}
			boolean b = isEffectiveDate(baoXian.getStartDays(), beginDate, endDate);
			if (b) {
				return baoXian;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 通过洗美1表找到2表
	 * 
	 * @return
	 */
	public XiMei2 findByXiMei(XiMei baoXian1, Date beginDate, Date endDate) {

		List<XiMei2> baoXian2 = ximei2Service.findByXiMei(baoXian1);

		if (baoXian2 != null) {
			XiMei2 baoXian = new XiMei2();
			Integer id = 0;
			for (XiMei2 y : baoXian2) {
				if (id < y.getId()) {
					id = y.getId();
					baoXian = y;
				}
			}
			boolean b = isEffectiveDate(baoXian.getBeginDate(), beginDate, endDate);
			if (b) {
				return baoXian;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 通过延保1表找到2表
	 * 
	 * @return
	 */
	public YanBao2 findByYanBao(YanBao baoXian1, Date beginDate, Date endDate) {
		Integer yanBaoid = baoXian1.getId();
		List<YanBao2> baoXian2 = yanbaoService2.findByYanBaoId(yanBaoid);
		if (baoXian2 != null) {
			YanBao2 baoXian = new YanBao2();
			Integer id = 0;
			for (YanBao2 y : baoXian2) {
				if (id < y.getId()) {
					id = y.getId();
					baoXian = y;
				}
			}
			boolean b = isEffectiveDate(baoXian.getBeginDate(), beginDate, endDate);
			if (b) {
				return baoXian;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 通过保险1表找到2表
	 * 
	 * @return
	 */
	public Insurance2 findByBaoXian(Insurance baoXian1, Date beginDate, Date endDate) {
		List<Insurance2> baoXian2 = baoXianService2.findByInsurance(baoXian1);
		if (baoXian2 != null) {
			Insurance2 baoXian = new Insurance2();
			Integer id = 0;
			for (Insurance2 y : baoXian2) {
				if (id < y.getId()) {
					id = y.getId();
					baoXian = y;
				}
			}
			boolean b = isEffectiveDate(baoXian.getBeginDate(), beginDate, endDate);
			if (b) {
				return baoXian;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * @param sale1
	 * @return 通过售后前台1表找2表
	 */
	public AfterSalefront2 findByAfterSale1(AfterSalefront sale1, Date beginDate, Date endDate) {
		List<AfterSalefront2> sales2 = after2Service2.findByAfterSalefront(sale1);
		if (sales2 != null) {
			AfterSalefront2 sales = new AfterSalefront2();
			Integer id = 0;
			for (AfterSalefront2 y : sales2) {
				if (id < y.getId()) {
					id = y.getId();
				}
			}
			sales = after2Service2.findById(id);
			boolean b = isEffectiveDate(sales.getBeginDate(), beginDate, endDate);
			if (b) {
				return sales;
			} else {
				return null;
			}

		} else {
			return null;
		}
	}

	/**
	 * 
	 * @param nowTime   当前时间
	 * @param startTime 开始时间
	 * @param endTime   结束时间
	 * @return
	 * @author 判断当前时间在时间区间内
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
		if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
			return true;
		}

		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}

}
